<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 核销记录
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;

class Hexiao extends Common
{
    public function initialize(){
		parent::initialize();
	}
	//核销记录
    public function index(){
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = 'hexiao_order.'.input('param.field').' '.input('param.order');
			}else{
				$order = 'hexiao_order.id desc';
			}
			$where = [];
			$where[] = ['hexiao_order.aid','=',aid];
			$where[] = ['hexiao_order.bid','=',bid];
			
			if(input('param.nickname')) $where[] = ['member.nickname','like','%'.trim(input('param.nickname')).'%'];
			if(input('param.title')) $where[] = ['hexiao_order.title','like','%'.trim(input('param.title')).'%'];
			if(input('param.mid')) $where[] = ['hexiao_order.mid','=',trim(input('param.mid'))];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['hexiao_order.status','=',input('param.status')];
			$count = 0 + Db::name('hexiao_order')->alias('hexiao_order')->field('member.nickname,member.headimg,hexiao_order.*')->join('member member','member.id=hexiao_order.mid')->where($where)->count();
			$data = Db::name('hexiao_order')->alias('hexiao_order')->field('member.nickname,member.headimg,hexiao_order.*')->join('member member','member.id=hexiao_order.mid')->where($where)->page($page,$limit)->order($order)->select()->toArray();

			$typeArr = ['coupon'=>'优惠券','shop'=>'商城商品','scoreshop'=>'积分兑换商品','seckill'=>'秒杀商品','kanjia'=>'砍价商品','lucky_collage'=>'幸运拼团商品','collage'=>'拼团商品','tuangou'=>'团购商品','choujiang'=>'抽奖活动奖品','kanjia'=>'砍价商品'];

			foreach($data as $k=>$v){
				$data[$k]['typename'] = ($v['type'] ? $typeArr[$v['type']] : '');
			}
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}
		return View::fetch();
    }
	//导出
	public function excel(){
		if(input('param.field') && input('param.order')){
			$order = 'hexiao_order.'.input('param.field').' '.input('param.order');
		}else{
			$order = 'hexiao_order.id desc';
		}
		$where = array();
		$where[] = ['hexiao_order.aid','=',aid];
		$where[] = ['hexiao_order.bid','=',bid];
		
		if(input('param.nickname')) $where[] = ['member.nickname','like','%'.trim(input('param.nickname')).'%'];
		if(input('param.title')) $where[] = ['hexiao_order.title','like','%'.trim(input('param.title')).'%'];
		if(input('param.mid')) $where[] = ['hexiao_order.mid','=',trim(input('param.mid'))];
		if(input('?param.status') && input('param.status')!=='') $where[] = ['hexiao_order.status','=',input('param.status')];
		$list = Db::name('hexiao_order')->alias('hexiao_order')->field('member.nickname,member.headimg,hexiao_order.*')->join('member member','member.id=hexiao_order.mid')->where($where)->order($order)->select()->toArray();
		$title = array();
		$title[] = 'ID';
		$title[] = t('会员').'信息';
		$title[] = '订单号';
		$title[] = '订单类型';
		$title[] = '核销内容';
		$title[] = '核销时间';
		$title[] = '备注信息';
		$data = array();
		$typeArr = ['coupon'=>'优惠券','shop'=>'商城商品','scoreshop'=>'积分兑换商品','seckill'=>'秒杀商品','kanjia'=>'砍价商品','lucky_collage'=>'幸运拼团商品','collage'=>'拼团商品','tuangou'=>'团购商品','choujiang'=>'抽奖活动奖品','kanjia'=>'砍价商品'];

		foreach($list as $v){
			$tdata = array();
			$tdata[] = $v['id'];
			$tdata[] = $v['nickname'].'('.t('会员').'ID:'.$v['mid'].')';
			$tdata[] = ' '.$v['ordernum'];
			$tdata[] = ($v['type'] ? $typeArr[$v['type']] : '');
			$tdata[] = $v['title'];
			$tdata[] = date('Y-m-d H:i:s',$v['createtime']);
			$tdata[] = $v['remark'];
			$data[] = $tdata;
		}
		$this->export_excel($title,$data);
	}
	//删除
	public function del(){
		$ids = input('post.ids/a');
		Db::name('hexiao_order')->where('aid',aid)->where('id','in',$ids)->delete();
		\app\common\System::plog('删除核销记录'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}
}
